<?php

class NotifikasiModel extends Model {

    function getFilename($filename) {
        $pos = strripos($filename, '.');
        if ($pos === false) {
            return $filename;
        } else {
            return substr($filename, 0, $pos);
        }
    }

    function getExtension($filename) {
        $pos = strripos($filename, '.');
        if ($pos === false) {
            return "";
        } else {
            return substr($filename,$pos);
        }
    }

    function getUserId($username){
        $result = $this->query('select idUser from user where Username=\''.$username.'\'');
        if(count($result) != 0){
            return $result[0]['User']['idUser'];
        }else{
            return null;
        }
    }
    function getName($iduser){
        $result = $this->query('select username from user where idUser=\''.$iduser.'\'');
        if(count($result) != 0){
            return $result[0]['User']['username'];
        }else{
            return null;
        }
    }
    function getFullName($iduser){
        $result = $this->query('select fullname from user where idUser=\''.$iduser.'\'');
        if(count($result) != 0){
            return $result[0]['User']['fullname'];
        }else{
            return null;
        }
    }
    function getProfPic($iduser){
        $result = $this->query('select pp from user where idUser=\''.$iduser.'\'');
        if(count($result) != 0){
            return $result[0]['User']['pp'];
        }else{
            return null;
        }
    }
    function insert($from,$to,$msg){
        $query = "INSERT INTO chat (idFrom,idTo,content) VALUES (".$from.",".$to.",'".$msg."')";
        $result = $this->query($query);
        echo $this->getError();
        if (!$result) {
            $message = 'Invalid query: ' . $this->getError() . "\n";
            $message .= 'Whole query: ' . $query;
            return ($message);
        }else{
            return 'Sent Successfully';
        }
    }
    function getWhoOnline($myid){
        $query = 'select * from user,follow_user where follow_user.idUser='.$myid.' and follow_user.idFollowed=user.idUser and user.status=\'1\' and user.idUser<>'.$myid;
        $result = $this->query($query);
        $hasil = array();
        $counter = 0;
        foreach ($result as $value) {
            $hasil[$counter]['profpic'] = $value['User']['pp'];
            $hasil[$counter]['username'] = $value['User']['username'];
            $counter++;
        }
        return $hasil;
    }
    function fetch($from,$to){
        $query = 'select * from chat where (idFrom='.$from.' and idTo='.$to.') or (idFrom='.$to.' and idTo='.$from.') order by date desc';
        $result = $this->query($query);
        $hasil = array();
        $counter = 0;
        foreach ($result as $value) {
            $hasil[$counter]['msg'] = $value['Chat']['content'];
            $hasil[$counter]['sender'] = $this->getFullName($value['Chat']['idFrom']);
            $hasil[$counter]['date'] = $value['Chat']['date'];
            $counter++;
        }
        return $hasil;
    }

}
